<template>
  <el-row type="flex" justify="space-between" align="middle" style="height:40px;width:100%">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">修改部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartment } from '@/api/department'
export default {
  name: '',
  components: {},
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    handleCommand(type) {
      // 删除
      if (type === 'del') {
        // console.log(this.nodeData)
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示').then(async() => {
          // 1.ajax删除后端数据
          await reqDelDepartment(this.nodeData.id)
          // 2.删除成功后给提示
          this.$message.success('删除成功')
          // 子组件通知父组件重新获取数据( this.$emit() )
          this.$emit('del-depts')
        }).catch(() => {
          console.log('取消删除')
        })
      }

      // 添加
      if (type === 'add') {
        // 让添加的弹出框显示,携带数据  nodeData (父)
        this.$emit('show-add', this.nodeData)
      }

      // 编辑  nodeData (自己)
      if (type === 'edit') {
        this.$emit('show-edit', this.nodeData)
      }
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
